package com.twentyOne.ClothingLand.dao.impl;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.twentyOne.ClothingLand.dao.BaseDAO;
import com.twentyOne.ClothingLand.dao.IAccountBookDAO;
import com.twentyOne.ClothingLand.vo.AccountBook;

public class AccountBookDAO extends BaseDAO implements IAccountBookDAO {

	@Override
	public void addAccountBook(AccountBook accountBook) {
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.save(accountBook);
		tx.commit();
		session.close();
		
	}
	
	@Override
	public AccountBook searchAccountBookByInfo(int shopId, int type, int currency) {

		Session session = getSession();
		String hql = "from AccountBook as a where a.shopId=" + shopId + " and a.type=" + type + " and a.currency=" + currency;
		Query query = session.createQuery(hql);
		List<AccountBook> list = query.list();
		session.close();
		if (list.size() != 0) {
			AccountBook accountBook = (AccountBook) query.list().get(0);
			return accountBook;
		}else {
			return null;
		}
		
	}

	@Override
	public void modifiAccount(AccountBook accountBook) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		String hql = "update AccountBook set account=:account where accountBookId=:accountBookId";
		Query query = session.createQuery(hql);
		query.setDouble("account", accountBook.getAccount());
		query.setInteger("accountBookId", accountBook.getAccountBookId());
		query.executeUpdate();
		tx.commit();
		session.close();
		
	}

	@Override
	public List<AccountBook> searchAccountBookByShopId(int shopId) {

		Session session = getSession();
		String hql = "from AccountBook as a where a.shopId=" + shopId;
		Query query = session.createQuery(hql);
		List<AccountBook> list = query.list();
		session.close();
		if (list.size() != 0) {
			return list;
		}else {
			return null;
		}
		
	}

}
